	.module lcd1602.c
	.area text(rom, con, rel)
	.dbfile D:\01.Projects\JKElectronics\ATMEGA\CoreModule\sw\examples\lib\lcd1602.c
	.dbfunc e bsp_lcd1602_gpio_init _bsp_lcd1602_gpio_init fV
	.even
_bsp_lcd1602_gpio_init::
	.dbline -1
	.dbline 8
; 
; 
; #include "hw_config.h"
; #include "lcd1602.h"
; 
; 
; void bsp_lcd1602_gpio_init(void)
; {
	.dbline 9
; 	_delay_ms(300);				// wait for the internal reset 
	ldi R16,300
	ldi R17,1
	xcall __delay_ms
	.dbline 11
; #if 1
; 	sbi(LCD1602_DDR, LCD1602_RS_PIN_NO);
	lds R24,100
	ori R24,1
	sts 100,R24
	.dbline 12
; 	sbi(LCD1602_DDR, LCD1602_RW_PIN_NO);
	lds R24,100
	ori R24,2
	sts 100,R24
	.dbline 13
; 	sbi(LCD1602_DDR, LCD1602_EN_PIN_NO);
	lds R24,100
	ori R24,4
	sts 100,R24
	.dbline 20
; 
; 	//sbi(LCD1602_PORT, LCD1602_RS_PIN_NO);
; 	//sbi(LCD1602_PORT, LCD1602_RW_PIN_NO);
; 	//sbi(LCD1602_PORT, LCD1602_EN_PIN_NO);
; 
; 	// LCDa_CTRL_DDR |= _BV(LCDa_RS)|_BV(LCDa_RW)|_BV(LCDa_E);
; 	LCDa_DATA_DDR |= 0xFF;
	in R24,0x1a
	ori R24,255
	out 0x1a,R24
	.dbline -2
L3:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e bsp_lcd1602_init _bsp_lcd1602_init fV
	.even
_bsp_lcd1602_init::
	st -y,R10
	st -y,R11
	.dbline -1
	.dbline 29
; #endif
; #if 0
; 	LCDa_CTRL_DDR |= BIT(LCDa_RS)|BIT(LCDa_RW)|BIT(LCDa_E);
; 	LCDa_DATA_DDR |= 0xFF;
; #endif	
;  }
; 
; void bsp_lcd1602_init(void)
; {
	.dbline 32
; 
; 	
; 	LCD1602_sendbyte(iCmd, LCDa_FUNCTION);	// function mode setting 
	ldi R18,56
	clr R16
	xcall _LCD1602_sendbyte
L5:
	.dbline 33
; 	while(LCD1602_readBF());
L6:
	.dbline 33
	xcall _LCD1602_readBF
	tst R16
	brne L5
X0:
	.dbline 34
; 	LCD1602_sendbyte(iCmd, LCDa_ON);		// open the show 
	ldi R18,12
	clr R16
	xcall _LCD1602_sendbyte
L8:
	.dbline 35
; 	while(LCD1602_readBF());
L9:
	.dbline 35
	xcall _LCD1602_readBF
	tst R16
	brne L8
X1:
	.dbline 36
; 	LCD1602_clear();						// clear the screen 
	xcall _LCD1602_clear
L11:
	.dbline 37
; 	while(LCD1602_readBF());
L12:
	.dbline 37
	xcall _LCD1602_readBF
	mov R10,R16
	tst R16
	brne L11
X2:
	.dbline 38
; 	LCD1602_sendbyte(iCmd, LCDa_ENTRY);	// input mode setting 
	ldi R18,6
	clr R16
	xcall _LCD1602_sendbyte
	.dbline -2
L4:
	.dbline 0 ; func end
	ld R11,y+
	ld R10,y+
	ret
	.dbend
	.dbfunc e LCD1602_readbyte _LCD1602_readbyte fc
;          dByte -> R10
;         DatCmd -> R10
	.even
_LCD1602_readbyte::
	st -y,R10
	st -y,R11
	mov R10,R16
	.dbline -1
	.dbline 51
; }
; 
; 
; 
; 
; /******************************************* 
; Function name: LCD1602_readbyte 
; Function: From 1602 to read one byte of data or instructions 
; Parameters: DatCmd - when the data for the iDat, when instructions for the iCmd 
; Return Value: dByte - read back the data or instruction 
; ********************************************/ 
; byte LCD1602_readbyte(byte DatCmd)
; {
	.dbline 53
;  	byte dByte;
; 	if (DatCmd == iCmd)		 // Command operation 
	tst R10
	brne L15
X3:
	.dbline 54
; 		LCDa_CLR_RS;
	lds R24,101
	andi R24,254
	sts 101,R24
	xjmp L16
L15:
	.dbline 56
; 	else
; 		LCDa_SET_RS;
	lds R24,101
	ori R24,1
	sts 101,R24
L16:
	.dbline 58
; 		
; 	LCDa_SET_RW;	   	  	 // Read
	lds R24,101
	ori R24,2
	sts 101,R24
	.dbline 59
; 	LCDa_SET_E;
	lds R24,101
	ori R24,4
	sts 101,R24
	.dbline 60
; 	LCDa_DATA_DDR=0x00;	 // data bus is defined as the input 
	clr R2
	out 0x1a,R2
	.dbline 61
; 	dByte=LCDa_DI;			 // read data or instructions 
	in R10,0x19
	.dbline 62
; 	_delay_ms(2);				 // timing adjustment 
	ldi R16,2
	ldi R17,0
	xcall __delay_ms
	.dbline 63
; 	LCDa_CLR_E;	
	lds R24,101
	andi R24,251
	sts 101,R24
	.dbline 64
; 	LCDa_DATA_DDR|=0xff;	 // restore the output data bus 
	in R24,0x1a
	ori R24,255
	out 0x1a,R24
	.dbline 65
; 	return dByte;
	mov R16,R10
	.dbline -2
L14:
	.dbline 0 ; func end
	ld R11,y+
	ld R10,y+
	ret
	.dbsym r dByte 10 c
	.dbsym r DatCmd 10 c
	.dbend
	.dbfunc e LCD1602_sendbyte _LCD1602_sendbyte fV
;          dByte -> R10
;         DatCmd -> R12
	.even
_LCD1602_sendbyte::
	xcall push_xgset003C
	mov R10,R18
	mov R12,R16
	.dbline -1
	.dbline 76
; }
; 
; /******************************************* 
; Function name: LCD1602_sendbyte 
; Function: 1602 to write a byte of data or instructions 
; Parameters: DatCmd - when the data for the iDat, when instructions for the iCmd 
; dByte - 1602 for the write data or instructions 
; Return Value: None 
; ********************************************/ 
; void LCD1602_sendbyte(byte DatCmd, byte dByte)
; {
	.dbline 77
; 	if (DatCmd == iCmd)		// Command operation 
	tst R12
	brne L18
X4:
	.dbline 78
; 		LCDa_CLR_RS;
	lds R24,101
	andi R24,254
	sts 101,R24
	xjmp L19
L18:
	.dbline 80
; 	else
; 		LCDa_SET_RS;
	lds R24,101
	ori R24,1
	sts 101,R24
L19:
	.dbline 82
; 		
; 	LCDa_CLR_RW;			// write
	lds R24,101
	andi R24,253
	sts 101,R24
	.dbline 83
; 	LCDa_SET_E;
	lds R24,101
	ori R24,4
	sts 101,R24
	.dbline 84
; 	LCDa_DO = dByte;		// write data
	out 0x1b,R10
	.dbline 85
; 	_delay_ms(2);
	ldi R16,2
	ldi R17,0
	xcall __delay_ms
	.dbline 86
; 	LCDa_CLR_E;	
	lds R24,101
	andi R24,251
	sts 101,R24
	.dbline -2
L17:
	.dbline 0 ; func end
	xjmp pop_xgset003C
	.dbsym r dByte 10 c
	.dbsym r DatCmd 12 c
	.dbend
	.dbfunc e LCD1602_Char_Set _LCD1602_Char_Set fV
;       ptString -> R10,R11
;            Col -> R20
;            Row -> R12
	.even
_LCD1602_Char_Set::
	xcall push_xgsetF03C
	mov R20,R18
	mov R12,R16
	ldd R10,y+8
	ldd R11,y+9
	.dbline -1
	.dbline 98
; }
; 
; /******************************************* 
; Function name: LCD1602_sendstr 
; Function: 1602 to the ranks assigned to write a string 
; Parameters: Row - the specified line 
; Col - the specified column 
; ptString - string pointer 
; Return Value: None 
; ********************************************/ 
; void LCD1602_Char_Set(byte Row, byte Col, byte *ptString)
; {
	.dbline 99
; 	switch (Row)
	mov R22,R12
	clr R23
	cpi R22,2
	ldi R30,0
	cpc R23,R30
	breq L24
X5:
	xjmp L21
L24:
	.dbline 102
; 	{
; 		case 2:
; 			LCD1602_sendbyte(iCmd, LCDa_L2 + Col); break;	// write line 2 of the specified column 
	mov R18,R20
	subi R18,64    ; addi 192
	clr R16
	xcall _LCD1602_sendbyte
	.dbline 102
	xjmp L26
L21:
	.dbline 104
; 		default:
; 			LCD1602_sendbyte(iCmd, LCDa_L1 + Col); break;	// write the first line of the specified column 
	mov R18,R20
	subi R18,128    ; addi 128
	clr R16
	xcall _LCD1602_sendbyte
	.dbline 104
	xjmp L26
L25:
	.dbline 107
; 	}
; 	while((*ptString)!='\0')		 // string is not the end of 
; 	{
	.dbline 108
; 		LCD1602_sendbyte(iDat, *ptString++);
	movw R30,R10
	ld R18,Z+
	movw R10,R30
	ldi R16,1
	xcall _LCD1602_sendbyte
	.dbline 109
; 	}
L26:
	.dbline 106
	movw R30,R10
	ldd R2,z+0
	tst R2
	brne L25
X6:
	.dbline -2
L20:
	.dbline 0 ; func end
	xjmp pop_xgsetF03C
	.dbsym r ptString 10 pc
	.dbsym r Col 20 c
	.dbsym r Row 12 c
	.dbend
	.dbfunc e LCD1602_clear _LCD1602_clear fV
	.even
_LCD1602_clear::
	.dbline -1
	.dbline 118
; }
; /******************************************* 
; Function name: LCD1602_clear 
; Function: clear-screen LCD 1602 
; Parameters: None 
; Return Value: None 
; ********************************************/ 
; void LCD1602_clear(void)
; {
	.dbline 119
; 	LCD1602_sendbyte(iCmd,LCDa_CLS);
	ldi R18,1
	clr R16
	xcall _LCD1602_sendbyte
	.dbline 120
; 	_delay_ms(4); // clear the screen after the command is written, 2ms delay is necessary!!! 
	ldi R16,4
	ldi R17,0
	xcall __delay_ms
	.dbline -2
L28:
	.dbline 0 ; func end
	ret
	.dbend
	.dbfunc e LCD1602_readBF _LCD1602_readBF fc
;           busy -> R20
	.even
_LCD1602_readBF::
	st -y,R20
	st -y,R21
	.dbline -1
	.dbline 129
; }
; /******************************************* 
; Function name: LCD1602_readBF 
; Function: clear-screen LCD 1602 
; Parameters: None 
; Return Value: busy - busy is 1, 0 can receive instruction 
; ********************************************/ 
; byte LCD1602_readBF(void)
; {	  
	.dbline 131
; 	byte busy;
; 	busy=LCD1602_readbyte(iCmd);		// read back to the BF flag and address 
	clr R16
	xcall _LCD1602_readbyte
	mov R20,R16
	.dbline 132
; 	if(busy&0x80)						// Busy
	sbrs R16,7
	rjmp L30
X7:
	.dbline 133
; 		busy=1;
	ldi R20,1
	xjmp L31
L30:
	.dbline 135
; 	else	   							// not busy, you can write 
; 		busy=0;
	clr R20
L31:
	.dbline 136
; 	return busy;
	mov R16,R20
	.dbline -2
L29:
	.dbline 0 ; func end
	ld R21,y+
	ld R20,y+
	ret
	.dbsym r busy 20 c
	.dbend
	.dbfunc e LCD1602_wrCGRAM _LCD1602_wrCGRAM fV
;              i -> R20
;        ptCGRAM -> R10,R11
	.even
_LCD1602_wrCGRAM::
	xcall push_xgset300C
	movw R10,R16
	.dbline -1
	.dbline 146
; }
; 
; /******************************************* 
; Function name: LCD1602_wrCGRAM 
; Function: Write data to CGRAM 
; Parameters: ptCGRAM - CGRAM data pointer 
; Return Value: None 
; ********************************************/ 
; void LCD1602_wrCGRAM(const byte *ptCGRAM)
; {
	.dbline 148
; 	byte i;
; 	LCD1602_sendbyte(iCmd, LCDa_CGRAM_ADDR);  // write the first address of CGRAM 
	ldi R18,64
	clr R16
	xcall _LCD1602_sendbyte
	.dbline 149
; 	for (i = 0; i < LCDa_CGMAX; i++)		  // write the contents of 64 bytes of CGRAM 
	clr R20
	xjmp L36
L33:
	.dbline 150
; 	{
	.dbline 151
; 		LCD1602_sendbyte(iDat,ptCGRAM[i]);
	mov R30,R20
	clr R31
	add R30,R10
	adc R31,R11
	lpm R18,Z
	ldi R16,1
	xcall _LCD1602_sendbyte
	.dbline 152
; 	}
L34:
	.dbline 149
	inc R20
L36:
	.dbline 149
	cpi R20,64
	brlo L33
X8:
	.dbline -2
L32:
	.dbline 0 ; func end
	xjmp pop_xgset300C
	.dbsym r i 20 c
	.dbsym r ptCGRAM 10 pkc
	.dbend
	.dbfunc e LCD1602_disCGRAM _LCD1602_disCGRAM fV
;              i -> R20
;            Col -> R20
;            Row -> R10
	.even
_LCD1602_disCGRAM::
	xcall push_xgsetF00C
	mov R20,R18
	mov R10,R16
	.dbline -1
	.dbline 161
; }
; /******************************************* 
; Function name: LCD1602_disCGRAM 
; Function: display CGRAM contents of 64 bytes written on two lines of 32 bytes 
; Parameters: None 
; Return Value: None 
; ********************************************/ 
; void LCD1602_disCGRAM(byte Row,byte Col)
; {
	.dbline 163
; 	byte i;
; 	switch (Row)
	mov R22,R10
	clr R23
	cpi R22,2
	ldi R30,0
	cpc R23,R30
	breq L41
X9:
	xjmp L38
L41:
	.dbline 166
; 	{
; 		case 2:
; 			LCD1602_sendbyte(iCmd, LCDa_L2 + Col); break;	// write line 2 of the specified column 
	mov R18,R20
	subi R18,64    ; addi 192
	clr R16
	xcall _LCD1602_sendbyte
	.dbline 166
	xjmp L39
L38:
	.dbline 168
; 		default:
; 			LCD1602_sendbyte(iCmd, LCDa_L1 + Col); break;	//  write the first line of the specified column 
	mov R18,R20
	subi R18,128    ; addi 128
	clr R16
	xcall _LCD1602_sendbyte
	.dbline 168
L39:
	.dbline 170
; 	}
; 	for (i = 0;i <4;i++)   // display the contents of the first 32 bytes 
	clr R20
	xjmp L45
L42:
	.dbline 171
; 	{	 	
	.dbline 172
; 		LCD1602_sendbyte(iDat, i);		
	mov R18,R20
	ldi R16,1
	xcall _LCD1602_sendbyte
	.dbline 173
; 	}
L43:
	.dbline 170
	inc R20
L45:
	.dbline 170
	cpi R20,4
	brlo L42
X10:
	.dbline -2
L37:
	.dbline 0 ; func end
	xjmp pop_xgsetF00C
	.dbsym r i 20 c
	.dbsym r Col 20 c
	.dbsym r Row 10 c
	.dbend
; }
; 
; 
